import {createStore} from 'redux'

import { composeWithDevTools } from '@redux-devtools/extension';


let userInfo = localStorage.getItem('userInfo')

try{
    userInfo = JSON.parse(userInfo) || {}
}catch(err){
    userInfo = {}
}
const initState = {
    userInfo,
}
const enhancer = composeWithDevTools()
const reducer = function(state,{type,payload}){
    switch (type) {
        case 'login':
            localStorage.setItem('userInfo',JSON.stringify(payload))
            return {
                ...state,
                userInfo:payload
            }
    
        case 'logout':
            localStorage.removeItem('userInfo')
            return {
                ...state,
                userInfo:{}
            }
    }
    return state;
}
const store = createStore(reducer,initState,enhancer)

export default store;